package cn.tedu.hospitalqd.service.impl;

import cn.tedu.hospitalqd.ex.ServiceException;
import cn.tedu.hospitalqd.mapper.PharmacyMapper;
import cn.tedu.hospitalqd.pojo.dto.PharmacyAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.PharmacyUpdateDTO;
import cn.tedu.hospitalqd.pojo.entity.Pharmacy;
import cn.tedu.hospitalqd.pojo.vo.PharmacyListItemVO;
import cn.tedu.hospitalqd.pojo.vo.PharmacyStandardVO;
import cn.tedu.hospitalqd.service.PharmacyService;
import cn.tedu.hospitalqd.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PharmacyServiceImpl implements PharmacyService {

    @Autowired
    private PharmacyMapper pharmacyMapper;

    @Override
    public void addNew(PharmacyAddNewDTO pharmacyAddNewDTO){
        log.debug("开始处理【添加取药表】的业务，参数：{}",pharmacyAddNewDTO);
        String name = pharmacyAddNewDTO.getJfId();
        int count = pharmacyMapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}",name,count);
        if(count > 0){
            String message = "添加取药表失败，取药表编号已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Pharmacy pharmacy = new Pharmacy();
        BeanUtils.copyProperties(pharmacyAddNewDTO,pharmacy);
        log.debug("即将执行插入数据，参数：{}",pharmacy);
        int rows = pharmacyMapper.insert(pharmacy);
        if(rows != 1){
            String message = "添加取药表失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id){
        log.debug("开始处理【根据ID删除取药表】的业务，参数：{}",id);
        PharmacyStandardVO pharmacy = pharmacyMapper.getStandardById(id);
        if(pharmacy == null){
            String message = "删除取药表失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        log.debug("即将执行删除，参数：{}",id);
        int rows = pharmacyMapper.deleteById(id);
        if(rows != 1){
            String message = "删除取药表失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(Long id, PharmacyUpdateDTO pharmacyUpdateDTO){
        log.debug("开始处理【修改取药表详情】的业务，ID：{}，新数据：{}",id,pharmacyUpdateDTO);
        PharmacyStandardVO queryResult = pharmacyMapper.getStandardById(id);
        if(queryResult == null){
            String message = "修改取药表详情失败，尝试修改的取药表数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int count = pharmacyMapper.countByNameAndNotId(id,pharmacyUpdateDTO.getJfId());
        if(count > 0){
            String message = "修改取药表详情失败，取药表编号已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Pharmacy pharmacy = new Pharmacy();
        BeanUtils.copyProperties(pharmacyUpdateDTO,pharmacy);
        pharmacy.setId(id);
        log.debug("即将执行修改数据，参数：{}",pharmacy);
        int rows = pharmacyMapper.update(pharmacy);
        if(rows != 1){
            String message = "修改取药表详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public PharmacyStandardVO getStandardById(Long id){
        log.debug("开始处理【根据ID查询取药表详情】的业务，参数：{}",id);
        PharmacyStandardVO queryResult = pharmacyMapper.getStandardById(id);
        if(queryResult == null){
            String message = "查询取药表详情失败，取药表数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public List<PharmacyListItemVO> list(){
        log.debug("开始处理【查询取药表列表】的业务，无参数");
        List<PharmacyListItemVO> list = pharmacyMapper.list();
        return list;
    }
}
